package com.uuah.server.transformer;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.uuah.exception.AbstractUuahException;
import com.uuah.exception.UuahException;
import com.uuah.exception.lifecycle.InitialisationException;
import com.uuah.server.UuahServer;
import com.uuah.server.api.transformer.IBaseTransformer;
import com.uuah.server.api.transport.service.ITransportServiceDescriptor;
import com.uuah.server.config.i18n.ServerMessages;
import com.uuah.server.exception.transport.service.TransportFactoryException;

/**
 *
 * TODO comment ...
 *
 * <p>
 * update record:updator，update time，update content and version
 * </p>
 *
 * @author <a href="jonny_quan@hotmail.com">jonny</a>
 * @date 2009 2009-3-27 下午02:07:01
 * @version 1.0.0
 */
public class TransformerUtils {

	public static final String COMMA = ",";

	private static Logger logger = LoggerFactory
			.getLogger(AbstractTransformer.class);

	public static void initialiseAllTransformers(List transformers)
			throws InitialisationException {
		if (transformers != null) {
			Iterator transformer = transformers.iterator();
			while (transformer.hasNext()) {
				((IBaseTransformer) transformer.next()).initialise();
			}
		}
	}

	public static String toString(List transformers) {
		StringBuffer buffer = new StringBuffer();
		Iterator transformer = transformers.iterator();
		while (transformer.hasNext()) {
			buffer.append(transformer.next().toString());
			if (transformer.hasNext()) {
				buffer.append(" -> ");
			}
		}
		return buffer.toString();
	}

	public static IBaseTransformer firstOrNull(List transformers) {
		if (transformers != null && 0 != transformers.size()) {
			return (IBaseTransformer) transformers.get(0);
		} else {
			return null;
		}
	}

	protected static interface TransformerSource {
		public IBaseTransformer getTransformer()
				throws TransportFactoryException;
	}

	protected static IBaseTransformer getTransformerFromSource(
			TransformerSource source) {
		try {
			IBaseTransformer transformer = source.getTransformer();
			if (transformer != null) {
				transformer.initialise();
			}
			return transformer;
		} catch (AbstractUuahException e) {
			if (logger.isDebugEnabled()) {
				logger.debug(e.getMessage(), e);
			}
			return null;
		}
	}

	public static IBaseTransformer getDefaultRequestTransformer(
			final ITransportServiceDescriptor serviceDescriptor) {
		return getTransformerFromSource(new TransformerSource() {
			public IBaseTransformer getTransformer()
					throws TransportFactoryException {
				return serviceDescriptor.createRequestTransformer();
			}
		});
	}

	public static IBaseTransformer getDefaultResponseTransformer(
			final ITransportServiceDescriptor serviceDescriptor) {
		return getTransformerFromSource(new TransformerSource() {
			public IBaseTransformer getTransformer()
					throws TransportFactoryException {
				return serviceDescriptor.createResponseTransformer();
			}
		});
	}

	public static List getTransformers(String names) throws UuahException {
		if (null != names) {
			List transformers = new LinkedList();
			StringTokenizer st = new StringTokenizer(names, COMMA);
			while (st.hasMoreTokens()) {
				String key = st.nextToken().trim();
				IBaseTransformer transformer = UuahServer.getUuahContext()
						.getRegistry().lookupTransformer(key);

				if (transformer == null) {
					throw new UuahException(ServerMessages.objectNotRegistered(
							"Transformer", key));
				}
				transformers.add(transformer);
			}
			return transformers;
		} else {
			return null;
		}
	}

}
